<?php /* УТФ-8:fix */

class SPImodel__singlepage__manage extends SPImodel__singlepage
{
    public function get_by_tid( $tid )
    {
        $lang = SPI::I('lang')->get_lang();
		
		$query =    " SELECT id , t_created, tid, pid_profile_addby,
							name_ru, 
							name_ua, 
							name_en, 
							description_ru, 
							description_ua, 
							description_en, 
							meta_title_ua, 
							meta_title_ru, 
							meta_title_en, 
							meta_keywords_ua, 
							meta_keywords_ru, 
							meta_keywords_en, 
							meta_description_ru, 
							meta_description_ua, 
							meta_description_en, 
							name_{$lang} as name,
							description_{$lang} as description,
							meta_title_{$lang} as meta_title,
							meta_keywords_{$lang} as meta_keywords,
							meta_description_{$lang} as meta_description 
							"
               .      " FROM ".SPIcfg::$store['table:singlepages']
               .     " WHERE tid = '" . $tid ."';"
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function insert( $tid, $fields )
    {
		$fields['pid_profile_addby'] = SPI::I('Iam')->get_id();
        $fields['tid'] = $tid;

        $result = SPIstore::I()->insert(
            SPIcfg::$store['table:singlepages'],
            $fields,
            null,
            array( 'id' => SPIcfg::$store['sequence:global'] )
        );

        return $result === false ? false : $result['id'];
    }

    public function modify( $tid, $fields )
    {
		return SPIstore::I()->modify(
            SPIcfg::$store['table:singlepages'],
            $fields,
            array( 'tid' => $tid )
        );
    }

    public function save( $tid, $fields )
    {
        if( $this->get_by_tid( $tid ) === false )
        {
            return $this->insert( $tid, $fields );
        }
        else
        {
            return $this->modify( $tid, $fields );
        }
    }
}
